import request from '@/utils/request';
import { FlowPath, FlowPathFormValues } from '@/pages/backhub/flowpath/store/flowpathTypes';

// 获取工作流列表
export const getFlowPathList = async (params?: {
  page?: number;
  pageSize?: number;
  keyword?: string;
  status?: string;
}): Promise<FlowPath[]> => {
  const response = await request.get('/engine/starhub/flowpaths', { params });
  return response.data;
};

// 创建工作流
export const createFlowPath = async (data: FlowPathFormValues): Promise<FlowPath> => {
  const response = await request.post('/engine/starhub/flowpaths', data);
  return response.data;
};

// 更新工作流
export const updateFlowPath = async (id: string, data: FlowPathFormValues): Promise<FlowPath> => {
  const response = await request.put(`/engine/starhub/flowpaths/${id}`, data);
  return response.data;
};

// 删除工作流
export const deleteFlowPath = async (id: string): Promise<void> => {
  await request.delete(`/engine/starhub/flowpaths/${id}`);
};

// 获取工作流详情
export const getFlowPathDetail = async (id: string): Promise<FlowPath> => {
  const response = await request.get(`/engine/starhub/flowpaths/${id}`);
  return response.data;
};

// 批量删除工作流
export const batchDeleteFlowPaths = async (ids: string[]): Promise<void> => {
  await request.delete('/engine/starhub/flowpaths/batch', { data: { ids } });
};

// 更新工作流状态
export const updateFlowPathStatus = async (id: string, status: string): Promise<void> => {
  await request.patch(`/engine/starhub/flowpaths/${id}/status`, { status });
};

// 批量更新工作流状态
export const batchUpdateFlowPathStatus = async (ids: string[], status: string): Promise<void> => {
  await request.patch('/engine/starhub/flowpaths/batch/status', { ids, status });
};

// 验证工作流名称是否可用
export const checkFlowPathName = async (name: string): Promise<boolean> => {
  const response = await request.get('/engine/starhub/flowpaths/check-name', {
    params: { name }
  });
  return response.data.available;
};

// 获取工作流执行统计
export const getFlowPathStatistics = async (id: string, params?: {
  startTime?: string;
  endTime?: string;
}): Promise<{
  totalExecutions: number;
  successExecutions: number;
  failedExecutions: number;
  averageExecutionTime: number;
  nodeStatistics: {
    nodeId: string;
    executionCount: number;
    successCount: number;
    failureCount: number;
    averageExecutionTime: number;
  }[];
}> => {
  const response = await request.get(`/engine/starhub/flowpaths/${id}/statistics`, { params });
  return response.data;
};

// 执行工作流
export const executeFlowPath = async (id: string, input: Record<string, any>): Promise<{
  executionId: string;
  status: string;
  result?: any;
  error?: string;
}> => {
  const response = await request.post(`/engine/starhub/flowpaths/${id}/execute`, input);
  return response.data;
};

// 获取工作流执行结果
export const getFlowPathExecutionResult = async (executionId: string): Promise<{
  status: string;
  result?: any;
  error?: string;
  logs: {
    nodeId: string;
    timestamp: string;
    level: string;
    message: string;
  }[];
}> => {
  const response = await request.get(`/engine/starhub/flowpaths/executions/${executionId}`);
  return response.data;
};